<?php

function parse_csv_file($file, $columnheadings = false, $delimiter = ',', $enclosure = "\"", $removeQuotes=true, $CATa="",$type="posts") {

	global $wpdb;
	
	if($type =="users"){
	require_once(ABSPATH . WPINC . '/registration.php');
	}
		 
	$post_fields = array('SKU','category','tags','post_author','post_date','post_date_gmt','post_content','post_title','post_excerpt','post_status','comment_status','ping_status','post_password','post_name','to_ping','pinged','post_modified','post_modified_gmt','post_content_filtered','post_parent','guid','menu_order','post_type','post_mime_type','comment_count');
	$bad_fields = array('ID');
	
	$AddtoCats = array();
	$user_fields = array('fname','lname','email','password','website');	
	$description="";
	$ShopperPressID=0; $ShopperPressUpdatedID =0;
	$row = 1;
	$rows = array();
	$handle = fopen($file, 'r');
	$customString = array();
	// GET TITLES
	$titles = fgetcsv($handle,1000,$delimiter);

	$cats=""; if(is_array($_POST['csv']['cat'])){ foreach($_POST['csv']['cat'] as $cat){ $cats .= $cat.",";	} }
 

	while(!feof($handle)) {	
	
	$line = fgetcsv($handle, 4096);

		if($type =="posts"){
	
			 if(is_array($line)){ foreach ($line as $key => $value) {
 		
				unset($data[$key]);
				
				if(!in_array(trim($titles[$key]), $post_fields)){	
				
					 if(!in_array($titles[$key],$bad_fields)){								 
					 
						$customString[$titles[$key]] = $value;
					
					}
	
				}else{
	  
					if(trim($titles[$key]) =="post_title"){								
						$title = $value;													
					}elseif(trim($titles[$key]) =="post_content"){					
						$description = $value;					
					}elseif(trim($titles[$key]) =="post_excerpt"){					
						$excerpt = $value;					
					}elseif(trim($titles[$key]) =="post_author"){					
						$author = $value;									
					}elseif(trim($titles[$key]) =="tags"){					
						$tags = $value;									
					}elseif(trim($titles[$key]) =="category"){					
						$category = $value;									
					}elseif(trim($titles[$key]) =="SKU"){					
						$SKU = $value;			 		
					}
					
					if($author =="" ){ $author=1; }
	
				} 			
				
		   } }

				// CHECK THIS PRODUCT DOESNT ALREADY EXIST	
				$CanContinue = true;			
				if(isset($SKU) && $SKU !=""){
				$SQL = "SELECT $wpdb->postmeta.post_id AS total FROM $wpdb->postmeta
				WHERE $wpdb->postmeta.meta_value = ('".$SKU."') AND $wpdb->postmeta.meta_key = 'SKU'
				LIMIT 1";
				$result = mysql_query($SQL, $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__);						
				$array = mysql_fetch_assoc($result);
				if(!empty($array['total'])) { $CanContinue = false;  }
				}
					 
				if ($CanContinue) {
				

					if(isset($category) && strlen($category) > 2){	
					$cats_array = explode(",",$category);
					foreach($cats_array as $Addcat){
						$args = array(
						'cat_name' => $Addcat ); 
						$catzID = wp_insert_term($Addcat, 'category', $args);
						array_push($AddtoCats,$catzID['term_id']);
					}					
					}
					 
					if($title != ""){			  
					  $my_post = array();
					  $my_post['post_title'] 	= $title;
					  $my_post['post_content'] 	= $description;
					  $my_post['post_excerpt'] 	= $excerpt;
					  $my_post['post_author'] 	= $author;
					  $my_post['post_status'] 	= "publish";
					  if(isset($tags)){
					   $my_post['tags_input'] 	= $tags;
					  }
					  // CATEGORY SETUP
					  if(empty($AddtoCats)){
						$my_post['post_category'] = $CATa;
					  }else{
						$my_post['post_category'] = $AddtoCats;
					  }
					  $POSTID = wp_insert_post( $my_post );			  
					  $AddtoCats = array();
					  if(is_array($customString)){ 
						foreach($customString as $key=>$value){						
							update_post_meta($POSTID,$key,$value);		 
					  }}

 					  if(isset($SKU) && $SKU !=""){
					  update_post_meta($POSTID,"SKU",$SKU);
 					  }

				      // UPDATE PRODUCT COUNTER
					  $ShopperPressID++; 
				} 

							  
			  }else{

				// CATEGORY SETUP
				if(isset($category) && strlen($category) > 2){	
				$cats_array = explode(",",$category);
				foreach($cats_array as $Addcat){
					$args = array(
					'cat_name' => $Addcat ); 
					$catzID = wp_insert_term($Addcat, 'category', $args);
					array_push($AddtoCats,$catzID['term_id']);
				}					
				}

				// UPDATE PRODUCT RATHER THAN ADDING ONE	
				$my_post = array();
				$my_post['ID'] 				= $array['total'];
				$my_post['post_title'] 		= $title;
				$my_post['post_content'] 	= $description;
				$my_post['post_excerpt'] 	= $excerpt;
				$my_post['post_author'] 	= $author;
				$my_post['post_status'] 	= "publish";
				if(isset($tags)){
					$my_post['tags_input'] 	= $tags;
				}
			   // CATEGORY SETUP
				if(empty($AddtoCats)){
						$my_post['post_category'] = $CATa;
			   }else{
						$my_post['post_category'] = $AddtoCats;
				}
				wp_update_post( $my_post );		
				$AddtoCats = array();
				if(is_array($customString)){ 
						foreach($customString as $key=>$value){						
							update_post_meta($array['total'],$key,$value);		 
				}}
 
				$ShopperPressUpdatedID++; 
			  }
				
	 
		}
		
		
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
 	if($type =="users"){
	
	
	if(is_array($line)){ foreach ($line as $key => $value) {
 
        unset($data[$key]);
        $value = $value;
 
		// REMOVE QUOTES
		if($removeQuotes){ $titles[$key] = str_replace('"','',$titles[$key]);  $value = str_replace('"','',$value); } //

		// IF NOT IN POST FIELDS ADD TO CUSTOM META TAGS
		if(!in_array($titles[$key], $user_fields)){		

			if(!in_array($titles[$key],$bad_fields)){								 
				$description .= $titles[$key].=" - ".$value."\r";
			}
		
		}else{
		
			if(trim($titles[$key]) =="email"){
			
			$email = $value;
			$username_bits = explode("@",$value);
			$username = $username_bits[0];
			
			}elseif(trim($titles[$key]) =="password"){
			
			$password = $value;	
			
			}elseif(trim($titles[$key]) =="fname"){
			
			$fname = $value;
			
			}elseif(trim($titles[$key]) =="lname"){
			
			$lname = $value;
			
			}elseif(trim($titles[$key]) =="website"){
			
			$website = $value;
			
			}	
		
		
		}	
	
	} }
	
	
	$userdata = array(	
	//'ID' => $USERID,
	'user_email' => $email,
	'display_name' => $fname, 
	'user_pass' => $password,
	'user_login' => $username,
	'first_name' => $fname,
	'last_name' => $lname,
	'description' => $description,
	'user_url' => $website
	);
	
	wp_insert_user( $userdata );
	
	$description="";
	
	$ShopperPressID++; 
	
	}
 
 
	
	 
	} /* end while */
 
	fclose($handle);
	return $ShopperPressID."**".$ShopperPressUpdatedID;
}

?>